package com.bt.dmdb.controller.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.bt.dmdb.controller.LoginController;
import com.bt.dmdb.model.LoginUser;
import com.bt.dmdb.service.UsersService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/")
public class LoginControllerImpl implements LoginController {

    @Resource
    private UsersService usersService;

    public UsersService getUsersService() {
        return usersService;
    }

    public void setUsersService(UsersService usersService) {
        this.usersService = usersService;
    }

    @RequestMapping
    public String homePage(HttpSession session) {
        return (session.getAttribute(NAME) != null) ? "redirect:/admin.html": LOGIN;
    }

    @Override
    @RequestMapping(value = "app", method = { RequestMethod.POST, RequestMethod.GET })
    public String authenticate(HttpSession session, @ModelAttribute(value = NAME) String name, @ModelAttribute(value = PASSWORD) String password) {
        if (session.getAttribute(NAME) != null) {
            return "/admin.html";
        } else/* if (NumberUtils.isNumber(name)) */{
        	LoginUser loginUser = usersService.validateUser(name, password);
            if (loginUser != null) {
                session.setAttribute(USER, loginUser);
                session.setAttribute(NAME, loginUser.getFirstname());
                return "redirect:/admin.html";
            } else {
                return LOGIN;
            }
        } /*else {
            return ERROR;
        }*/
    }
    
   // Admin section - start
    @Override
    @RequestMapping(value = "addUser", method = { RequestMethod.POST, RequestMethod.GET })
    public String addUser(@ModelAttribute(value = "firstName") String firstName, @ModelAttribute(value = "lastName") String lastName, @ModelAttribute(value = "emailId") String emailId, @ModelAttribute(value = PASSWORD) String password, @ModelAttribute(value = "role") String role, ModelMap m) {
    	usersService.addUser(firstName, lastName, emailId, password, role);
    	String name = SecurityContextHolder.getContext().getAuthentication().getName();
    	LoginUser loginUser = usersService.getUser(name);
    	m.addAttribute("uid", loginUser.getId());
    	m.addAttribute("uname", loginUser.getFirstname());
    	m.addAttribute("urname", loginUser.getRole().getName());
    	return "success-login";
    }
    
    @Override
    @RequestMapping(value = "editUser", method = { RequestMethod.POST, RequestMethod.GET })
    public String editUser(@ModelAttribute(value = "id") Long id, @ModelAttribute(value = "firstName") String firstName, @ModelAttribute(value = "lastName") String lastName, @ModelAttribute(value = "emailId") String emailId, @ModelAttribute(value = PASSWORD) String password, @ModelAttribute(value = "role") String role, ModelMap m) {
    	usersService.editUser(id, firstName, lastName, emailId, password, role);
    	String name = SecurityContextHolder.getContext().getAuthentication().getName();
    	LoginUser loginUser = usersService.getUser(name);
    	m.addAttribute("uid", loginUser.getId());
    	m.addAttribute("uname", loginUser.getFirstname());
    	m.addAttribute("urname", loginUser.getRole().getName());
    	return "success-login";
    }
    // Admin section - end
    
    public static final String NAME = "name", ADMIN = "admin", INDEX = "index", PASSWORD = "password", LOGIN = "login", ERROR = "errorPage", USER = "user";
}
